package com.funtl.screw.common.data.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.funtl.screw.common.data.filter.DruidSqlLogFilter;
import com.funtl.screw.common.data.permission.DataPermissionsInnerInterceptor;
import com.funtl.screw.common.data.permission.DataPermissionsService;
import com.funtl.screw.common.data.permission.DataPermissionsServiceImpl;
import com.funtl.screw.common.data.permission.DataPermissionsSqlInjector;
import com.funtl.screw.common.data.resolver.SqlFilterArgumentResolver;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;
import java.util.List;

/**
 * mybatis plus config
 *
 * @author Lusifer
 */
@Configuration
@ConditionalOnBean(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(MybatisSqlLogProperties.class)
public class MybatisPlusConfiguration implements WebMvcConfigurer {

    /**
     * 增加请求参数解析器，对请求中的参数注入SQL 检查
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolverList) {
        resolverList.add(new SqlFilterArgumentResolver());
    }

    @Bean
    @ConditionalOnMissingBean
    public DataPermissionsService dataPermissionsService() {
        return new DataPermissionsServiceImpl();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 数据权限
        DataPermissionsInnerInterceptor dataPermissionsInnerInterceptor = new DataPermissionsInnerInterceptor();
        dataPermissionsInnerInterceptor.setDataPermissionsService(dataPermissionsService());
        interceptor.addInnerInterceptor(dataPermissionsInnerInterceptor);
        // 分页支持
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    /**
     * 扩展 mybatis-plus baseMapper 支持数据权限
     */
    @Bean
    @ConditionalOnBean(DataPermissionsService.class)
    public DataPermissionsSqlInjector dataScopeSqlInjector() {
        return new DataPermissionsSqlInjector();
    }

    /**
     * SQL 日志格式化
     */
    @Bean
    public DruidSqlLogFilter sqlLogFilter(MybatisSqlLogProperties properties) {
        return new DruidSqlLogFilter(properties);
    }

    /**
     * 审计字段自动填充
     *
     * @return {@link MetaObjectHandler}
     */
    @Bean
    public MybatisPlusMetaObjectHandler mybatisPlusMetaObjectHandler() {
        return new MybatisPlusMetaObjectHandler();
    }
}
